#pragma once

#include "pch.h"
#include "prerequisites.h"
#include "resource.h"
#include "render_device.h"

class c_d3dx_mesh;
class c_d3dx_submesh
{
    friend class c_d3dx_mesh; 

public:
    ~c_d3dx_submesh(); 
    
    void set_material(const material_ptr& mat) { m_material = mat; }
    material_ptr get_material() const { return m_material; }
    
    unsigned long get_submesh_id() const {return m_att_id;} 
    
    void get_render_op(render_op& op);
    
private:
    c_d3dx_submesh(c_d3dx_mesh* parent_mesh, D3DXATTRIBUTERANGE *att_entry, const material_ptr& mat);
    
private: 
    c_d3dx_mesh *m_parent_mesh;

    unsigned long m_att_id;
    unsigned long m_face_start;
    unsigned long m_face_count; 
    unsigned long m_vertex_start; 
    unsigned long m_vertex_count;
    
    material_ptr m_material;
};
typedef boost::shared_ptr<c_d3dx_submesh> submesh_ptr;

//////////////////////////////////////////////////////////////////////////

class c_d3dx_mesh : public c_resource
{
public:
    typedef std::vector<submesh_ptr> submesh_vec_t;
    typedef submesh_vec_t::iterator submesh_iterator; 

    c_d3dx_mesh(const d3dx_mesh_ptr& d3dx_mesh);
    ~c_d3dx_mesh();

    unsigned int get_num_submeshes() const { return m_submeshes.size(); }
    submesh_iterator submesh_begin() { return m_submeshes.begin(); }
    submesh_iterator submesh_end() { return m_submeshes.end(); }
    
    d3dx_mesh_ptr& get_d3dx_mesh_ptr() { return m_d3dx_mesh; }
    
    d3d9_vb_ptr get_d3d9_vb_ptr(); 
    d3d9_ib_ptr get_d3d9_ib_ptr();
    
private:
 

private:
    // geometry
    d3dx_mesh_ptr m_d3dx_mesh;
    
    // submeshes
    submesh_vec_t m_submeshes; 
};

mesh_ptr create_mesh_from_x_file(const std::string& file_name);

/** Build a cube mesh
*   The origin point in model space is located at (width/2, height/2, depth/2)
*/
mesh_ptr make_simple_cube_mesh(float width, float height, float depth);


/** Build a room mesh
*   
*/ 
mesh_ptr make_room_mesh(float width, float height, float depth); 